//
//  LZChangPwdViewController.m
//  DoctorFAQs
//
//  Created by langZai on 2017/2/20.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "LZChangPwdViewController.h"
#import "LZChangPwdCell.h"

@interface LZChangPwdViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *mainTableView;
/** textField PlaceHolder */
@property (nonatomic , strong) NSArray *placeHolderArray;
/** 标题数组 */
@property (nonatomic , strong) NSArray *titleArray;
/** 原密码 */
@property (nonatomic, strong) NSString *firstPwd;
/** 新密码 */
@property (nonatomic, strong) NSString *newsPwd;
/** 再次输入的新密码 */
@property (nonatomic, strong) NSString *conNewPwd;
@end

static NSString *cellID = @"lz_changPwdCell";

@implementation LZChangPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KHColorFromRGB(0xf8f8f8);
    self.navigationItem.title = @"修改密码";
    _titleArray = @[@"原密码",@"新密码",@"确认新密码"];
    _placeHolderArray = @[@"填写原来密码",@"填写新密码",@"再次填写"];
    [self createTableView];
}




// 创建tableView
- (void)createTableView {
    
    // 保存按钮
    UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60*xsIZE, 30*ysIZE)];
    [saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] init];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.rowHeight = 60;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.backgroundColor = KHColorFromRGB(0xf8f8f8);
        [self.view addSubview:_mainTableView];
        [_mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.sizeOffset(CGSizeMake(SCREEN_Width, SCREEN_Height - 10));
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.view).offset(10*ysIZE);
        }];
    }
}

#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LZChangPwdCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[LZChangPwdCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
        cell.textLabel.text = _titleArray[indexPath.row];
        cell.contentTextField.placeholder = _placeHolderArray[indexPath.row];
    }
    return cell;
}

// 点击保存按钮
- (void)saveBtnClick {
    LZChangPwdCell *changCell0 = [_mainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    [changCell0.contentTextField resignFirstResponder];
    _firstPwd = changCell0.contentTextField.text;
    LZChangPwdCell *changCell1 = [_mainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    [changCell1.contentTextField resignFirstResponder];
    _newsPwd = changCell1.contentTextField.text;
    LZChangPwdCell *changCell2 = [_mainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    [changCell2.contentTextField resignFirstResponder];
    _conNewPwd = changCell2.contentTextField.text;
    
    if (!Predicate(PasswordRegex, _firstPwd) || ![_firstPwd isEqualToString:[LZUserInfo shareInfo].passWord]) {
        [YJProgressHUD showMessage:@"请输入正确的原密码" inView:self.view];
        return;
    }
    if (![_newsPwd isEqualToString:_conNewPwd]) {
        [YJProgressHUD showMessage:@"两次密码输入不一致" inView:self.view];
        return;
    }
    if ([_newsPwd isEqualToString:[LZUserInfo shareInfo].passWord] || [_conNewPwd isEqualToString:[LZUserInfo shareInfo].passWord]) {
        [YJProgressHUD showMessage:@"不能与原密码相同" inView:self.view];
        return;
    }
    if (!Predicate(PasswordRegex, _newsPwd)) {
        [YJProgressHUD showMessage:@"新密码格式错误" inView:self.view];
        return;
    }
    if (!Predicate(PasswordRegex, _conNewPwd)) {
        [YJProgressHUD showMessage:@"确认新密码格式错误" inView:self.view];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    
    NSDictionary *paramas = @{@"uid":[LZUserInfo shareInfo].uid,@"password":_newsPwd};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Personage/editPwd" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dict);
        if ([dict[@"code"] intValue] == 1) {
            //修改成功跳登录界面
            [weakSelf backHomeViewController];
            
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"修改失败" inView:self.view];
    }];
}


// 跳转首页
- (void)backHomeViewController {
    
    [LZUserInfo deleteUserInfo];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LogOut" object:nil];
    
    self.navigationController.tabBarController.selectedIndex = 0;
    [self.navigationController popToRootViewControllerAnimated:YES];
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"againLogin" object:nil];
}


@end
